package com.lc.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import reactor.core.publisher.Mono;

/**
 * Author:LiChuang
 * Date: 2022/8/8 14:36
 */
@Configuration
public class RequestLimitConfig {

    /*
    根据ip地址进行限流
    可以在该配置类中写多个限流方式，最终会优先使用带@Primary注解的先流方式
     */
    @Bean
    @Primary
    public KeyResolver ipKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getHeaders().getHost().getHostString());
    }

    /*
    根据api接口进行限流
     */
    @Bean
    public KeyResolver apiKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }

}
